package com.sxt.bus.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sxt.bus.constast.BUSConstast;
import com.sxt.bus.domain.Car;
import com.sxt.bus.domain.Customer;
import com.sxt.bus.domain.Rent;
import com.sxt.bus.service.CarService;
import com.sxt.bus.service.CustomerService;
import com.sxt.bus.service.RentService;
import com.sxt.bus.utils.ExportRentUtils;
import com.sxt.bus.vo.CustomerVo;
import com.sxt.bus.vo.RentVo;
import com.sxt.sys.utils.DataGridView;
import com.sxt.sys.utils.RandomUtils;
import com.sxt.sys.utils.WebUtils;

@Controller
@RequestMapping("rent")
public class RentController {
  @Autowired
	private RentService rentService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private CarService carservice;
	/**
	 * 跳转到客户管理的页面
	 */
	@RequestMapping("toRentCarManager")
	public String toRentCarManager(){
		return "business/rentCarManager";
	}
	@RequestMapping("loadCustomerByIdentity")
	@ResponseBody
	public Customer loadCustomerByIdentity(CustomerVo customerVo){
		Customer customer = customerService.selectByPrimaryKey(customerVo.getIdentity());
		return customer;
		
	}
	@RequestMapping("loadRentByRentId")
	@ResponseBody
	public Rent loadRentByRentId(RentVo rentVo){
		Rent rent = rentService.selectByPrimaryKey(rentVo.getRentid());
		return rent;
		
	}
	
	/**
	 * 加载初始化出租单数据
	 */
    @RequestMapping("initRentForm")
	@ResponseBody
    public RentVo initRentForm(RentVo rentVo){
    	rentVo.setRentid(RandomUtils.createRandomStringUsePreFix(BUSConstast.RENT_ID_PREFIX));
    	//设置操作员
    	rentVo.setOpername(WebUtils.getCurrentUser().getRealname());
    	
    	//设置出租状态
    	rentVo.setRentflag(BUSConstast.RENT_RENTFLAG_FALSE);
    	//设置起租时间
    	rentVo.setBegindate(new Date());
    	
    	
		return rentVo;
    }
    @RequestMapping("addRent")
	@ResponseBody
	public Map<String,Object> addRent(RentVo rentVo){
		Map<String,Object>  map=new HashMap<>();
		try {
			rentService.addRent(rentVo);
			map.put("msg", "添加成功");
		} catch (Exception e) {
			map.put("msg", "添加失败");
			e.printStackTrace();
		}
		return map;
	}
    
    //-------------------------------车辆出租管理--------------
    @RequestMapping("toRentManager")
	public String toRentManager(){
		return "business/rentManager";
	}
    //查询加载表单
    @RequestMapping("loadAllRent")
    @ResponseBody
    public DataGridView loadAllRent(RentVo rentVo){
		return rentService.queryAllRent(rentVo);
	}
    
  //查询加载表单
    @RequestMapping("updateRent")
    @ResponseBody
    public Map<String,Object> updateRent(RentVo rentVo){
    	Map<String,Object> map=new HashMap<>();
    	try {
    		this.rentService.updateByPrimaryKeySelective(rentVo);
			map.put("msg","修改成功");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map.put("msg","修改失败");
		}
		return map;
	}
    //查询加载表单
    @RequestMapping("deleteRent")
    @ResponseBody
    public Map<String,Object> deleteRent(RentVo rentVo){
    	Map<String,Object> map=new HashMap<>();
    	try {
    		this.rentService.deleteRent(rentVo);
    		map.put("msg","修改成功");
    	} catch (Exception e) {
    		// TODO: handle exception
    		e.printStackTrace();
    		map.put("msg","修改失败");
    	}
    	return map;
    }
    
    //导出出租单信息
    @RequestMapping("exportRent")
    public void exportRent(RentVo rentVo,HttpServletResponse response){
    	//查询出出租单信息
    	Rent rent=this.rentService.selectByPrimaryKey(rentVo.getRentid());
    	//查询客户信息
    	Customer customer=this.customerService.selectByPrimaryKey(rent.getIdentity());
    	//查询车辆信息
    	Car car=this.carservice.selectByPrimaryKey(rent.getCarnumber());
    	//组装文件名
    	String fileName=customer.getCustname()+"-出租单信息.xls";
    	ExportRentUtils.export(rent, customer, car, fileName, response);
    	
    }
}
